package com.java.zhuiyun.api.houserank.service.impl;

import com.java.zhuiyun.api.houserank.entity.HouseRank;
import com.java.zhuiyun.api.houserank.mapper.HouserankMapper;
import com.java.zhuiyun.api.houserank.model.Modelsize;
import com.java.zhuiyun.api.houserank.service.HouserankService;
import com.java.zhuiyun.api.playerinformation.entity.PlayerInformation;
import com.java.zhuiyun.api.playerinformation.mapper.PlayerInformationMapper;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.UploadFileUtils;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @ClassName HouserankServiceImpl
 * @Description TODO
 * @Author wuwan
 * @Date 2023/9/14 9:55
 **/
@Service
public class HouserankServiceImpl implements HouserankService {
    @Autowired
    private HouserankMapper houserankMapper;
    @Autowired
    private PlayerInformationMapper playerInformationMapper;
    /*
    * @Author wuwan
    * @Description 房屋数据信息查询
    * @Date 2023/10/12 10:40
    * @Return
    */
    @Override
    public CommonResult selectHouserank(PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(houserankMapper.selectHouserank());
        return CommonResult.ok(pageResult);
    }
    /*
    * @Author wuwan
    * @Description 房屋信息添加
    * @Date 2023/10/12 10:40
    * @Return
    */
    @Override
    public CommonResult addHouserank(MultipartFile file, HouseRank houseRank) {
        String s = UploadFileUtils.uploadFile(file);
        houseRank.setHouseImg(Integer.parseInt(s));
        Modelsize modelsize = houserankMapper.selectHouserankCount();
        houseRank.setHouseLevel(modelsize.getSizes()+1);
        houseRank.setCreateTime(new Date());
        houseRank.setUpdateTime(new Date());
        Integer integer = houserankMapper.addHouserank(houseRank);
        if (integer >= 1) {
            return CommonResult.ok("角色添加成功");
        }
        return CommonResult.error("角色添加失败");
    }
    /*
    * @Author wuwan
    * @Description 房屋信息修改
    * @Date 2023/10/12 10:40
    * @Return
    */
    @Override
    @Transactional
    public CommonResult updateHouserank(MultipartFile file, HouseRank houseRank) {
        if (file == null){
            houseRank.setUpdateTime(new Date());
            Integer integer = houserankMapper.updateHouseranks(houseRank);
            if (integer >= 1) {
                return CommonResult.ok("道具修改成功");
            }
            return CommonResult.error("道具修改失败");
        }
        String s = UploadFileUtils.uploadFile(file);
        houseRank.setHouseImg(Integer.parseInt(s));
        houseRank.setUpdateTime(new Date());
        Integer integer = houserankMapper.updateHouserank(houseRank);
        if (integer >= 1) {
            return CommonResult.ok("道具修改成功");
        }
        return CommonResult.error("道具修改失败");
    }
    /*
    * @Author wuwan
    * @Description 查询当前用户房屋信息
    * @Date 2023/10/12 10:41
    * @Return
    */
    @Override
    public CommonResult getHouseData(String openid) {
        // 查询用户等级
        PlayerInformation playerInformation = playerInformationMapper.selectPlayerHouseLevel(openid);
        Integer currentPlayerHouseLevel = playerInformation.getCurrentPlayerHouseLevel();
        // 根据等级查询出房屋等级对应的图片
        HouseRank houseData = houserankMapper.getHouseData(currentPlayerHouseLevel);
        // 升级所需积分
        Integer houseNeedLevel = houseData.getHouseNeedLevel();
        // 当前用户已有积分
        Integer currentPlayerScore = playerInformation.getCurrentPlayerScore();
        Integer i = houseNeedLevel - currentPlayerScore;
        houseData.setSxLevel(i);
        return CommonResult.ok(houseData);
    }
}
